package com.example.xd.myapplication;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;


@Route(path = "/myapplication/PermissionTestActivity")
public class TestPermissionActivity extends AppCompatActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {

    private TextView mTv_conten;
    private String[] mPermission;
    private String mPath;
    private Bundle mExtras;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_permission);
        mPermission = getIntent().getStringArrayExtra("permission");
        mPath = getIntent().getStringExtra("path");
        mExtras = getIntent().getBundleExtra("extras");
        mTv_conten = findViewById(R.id.tv_conten);
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < mPermission.length; i++) {
//            sb.append(mPermission[i] + "---");
//        }
//        Log.i("xd----", mPath);
//        mTv_conten.setText(sb.toString());
//        findViewById(R.id.cancle_btn).setOnClickListener(this);
//        findViewById(R.id.ok_btn).setOnClickListener(this);
        EasyPermissions.requestPermissions(this, "获取部分权限", 1, mPermission);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancle_btn:
                Toast.makeText(this, "没有权限，部分功能无法体验。", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.ok_btn:
                EasyPermissions.requestPermissions(this, "", 1, mPermission);
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        ARouter.getInstance().build(mPath).with(mExtras).navigation();
        finish();
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        Toast.makeText(this, "没有权限，部分功能无法体验。", Toast.LENGTH_SHORT).show();
        finish();

    }
}
